package com.miukoo.aikao.metadata.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miukoo.aikao.core.dtos.*;
import com.miukoo.aikao.core.utils.ApiUtils;
import com.miukoo.aikao.core.utils.AppUserUtils;
import com.miukoo.aikao.metadata.dto.SysUserDto;
import com.miukoo.aikao.metadata.dto.request.SysUserSaveReqDto;
import com.miukoo.aikao.metadata.pojos.SysUser;
import com.miukoo.aikao.metadata.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 管理用户控制器
 * </p>
 *
 * @author 聪明笨狗
 * @since 2020-04-13 16:57
 */
@Api(tags = {"管理用户"})
@RestController
@RequestMapping("/metadata/sys/v1")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

//    /**
//     * 用户登录
//     * @return
//     */
//    @LogInject(title = "登录系统")
//    @CrossOrigin
//    @ApiOperation(value = "用户登录")
//    @RequestMapping(value = "/login", method = {RequestMethod.POST})
//    public ApiResult<SysUserLoginDto> login(@RequestBody SysUserLoginReqDto reqDto) {
//        SysUserLoginDto respDto = sysUserService.login(reqDto.getUsername(), reqDto.getPassword());
//        return ApiUtils.success(respDto);
//    }

//    /**
//     * 用户登录
//     * @return
//     */
//    @CrossOrigin
//    @ApiOperation(value = "用户登录")
//    @RequestMapping(value = "/logout", method = {RequestMethod.POST})
//    public ApiResult logout(HttpServletRequest request) {
//        String token = request.getHeader("token");
//        System.out.println("+++++当前会话为："+token);
//        sysUserService.logout(token);
//        return ApiUtils.success();
//    }

//    /**
//     * 获取会话
//     * @return
//     */
//    @ApiOperation(value = "获取会话")
//    @RequestMapping(value = "/info", method = {RequestMethod.POST})
//    public ApiResult info(@RequestParam("token") String token) {
//        SysUserLoginDto respDto = sysUserService.token(token);
//        return ApiUtils.success(respDto);
//    }

    /**
     * 修改用户资料
     * @return
     */
    @ApiOperation(value = "修改用户资料")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public ApiResult update(@RequestBody SysUserDto reqDto) {
        Long userId = AppUserUtils.getUser().getId();
        return sysUserService.update(reqDto,userId);
    }


    /**
     * 保存或修改系统用户
     * @return
     */
    @ApiOperation(value = "保存或修改")
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public ApiResult save(@RequestBody SysUserSaveReqDto reqDto) {
        sysUserService.save(reqDto);
        return ApiUtils.success();
    }


    /**
     * 批量删除
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiResult edit(@RequestBody BaseIdsReqDto reqDto) {
        //根据ID删除
        sysUserService.removeByIds(reqDto.getIds());
        return ApiUtils.success();
    }

    /**
     * 分页查找
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiResult<PageResDto<List<SysUser>>> paging(@RequestBody PageReqDto<SysUserDto> reqDto) {

        //分页查询并转换
        PageResDto<List<SysUser>> page = sysUserService.paging(reqDto);
        return ApiUtils.success(page);
    }

    /**
     * 修改状态
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "修改状态")
    @RequestMapping(value = "/state", method = { RequestMethod.POST})
    public ApiResult state(@RequestBody BaseStateReqDto reqDto) {

        // 条件
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(SysUser::getId, reqDto.getIds())
                .ne(SysUser::getUserName, "admin");


        SysUser record = new SysUser();
        record.setState(reqDto.getState());
        sysUserService.update(record, wrapper);

        return ApiUtils.success();
    }


    /**
     * 保存或修改系统用户
     * @return
     */
    @ApiOperation(value = "学员注册")
    @RequestMapping(value = "/reg", method = {RequestMethod.POST})
    public ApiResult<Long> reg(@RequestBody SysUserDto reqDto) {
        SysUser respDto = sysUserService.reg(reqDto);
        return ApiUtils.success(respDto.getId());
    }

    /**
     * 快速注册，如果手机号存在则登录，不存在就注册
     * @return
     */
    @ApiOperation(value = "快速注册")
    @RequestMapping(value = "/quick-reg", method = {RequestMethod.POST})
    public ApiResult<SysUser> quick(@RequestBody SysUserDto reqDto) {
        SysUser respDto = sysUserService.quickReg(reqDto);
        return ApiUtils.success(respDto);
    }
}
